﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UICore;

public class Foe : MonoBehaviour {


    public float speed =10f;
    GameObject effectPre;
    //声明子弹预制体
    GameObject foeBulletPre;
    //声明子弹生成的位置
    Transform foeStartPos1;
    Transform foeStartPos2;
    Transform foeStartPos3;


    bool frag = true;
    int timer = 0;

	// Use this for initialization
	void Start () 
    {
        foeBulletPre = Resources.Load<GameObject>("FoeBullet");
        foeStartPos1 = transform.Find("FoeStartPos1");
        foeStartPos2 = transform.Find("FoeStartPos2");
        foeStartPos3 = transform.Find("FoeStartPos3");
        effectPre = Resources.Load<GameObject>("explosion_enemy");
     
	}
	
	// Update is called once per frame
	void Update () 
    {
        Move();
       
        timer++;
        if (timer % 30 == 0)
        {
            Attack();
        }
	}
       void Move()
       {
           
           if (transform.position.x >=13 || transform.position.x <= -13)
           {

               frag = !frag;
           }
           if (frag==true)
           {

               transform.position = transform.position + new Vector3(1, 0, 0) * speed * Time.deltaTime;
            
           
           }
           if(frag==false)
           {
               transform.position = transform.position + new Vector3(-1, 0, 0) * speed * Time.deltaTime;
       
           }

       }
       void  Attack()
        {

            Instantiate(foeBulletPre, foeStartPos1.position, foeBulletPre.transform.rotation);
            Instantiate(foeBulletPre, foeStartPos2.position, foeBulletPre.transform.rotation);
            Instantiate(foeBulletPre, foeStartPos3.position, foeBulletPre.transform.rotation);
        }

       //如果敌机撞到子弹
       void OnCollisionEnter(Collision other)
       {
           if (other.gameObject.tag == "PlayerBullet")
           {
               if (UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSlider2Value() <= 0.5f)
               {
                   UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().SetSlider2Value();//设置敌人红条为0
                   Destroy(this.gameObject);//销毁敌机
                   Destroy(other.gameObject);//销毁子弹
                   Instantiate(effectPre, this.transform.position, Quaternion.identity);
                   PlayUI.PassLevel();//通关
                   
               }
               else
               {
                   Instantiate(effectPre, this.transform.position, Quaternion.identity);
                   //减少红条
                   UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().ReduceFoeRed(0.5f);
                   if (UIManager.Instance.GetUIPanel(E_UiId.PlayUI).GetComponent<PlayUI>().GetSlider2Value() <= 0)
                   {
                       Destroy(this.gameObject);//销毁敌机
                       Destroy(other.gameObject);//销毁子弹
                       PlayUI.PassLevel();//通关
                     
                   }

               }
           }
       }


}
